package best.driver.android.common.model;

import best.driver.android.common.network.ApiRequest;

import java.util.ArrayList;
import java.util.List;

public abstract class PageRequestModel<T> extends HttpRequestModel {

    public static interface PageRequestModelListener<T> extends ModelListener {

        public void onModelDidLoadPage(int pageNumber, List<T> objects, boolean hasNextPage);

    }

    private int mPageSize;
    private int mCurrentPage;
    private boolean mHasMoreData = false;
    private ArrayList<T> mObjects;

    public PageRequestModel(int pageSize) {
        this.mPageSize = pageSize;
    }

    @Override
    protected ApiRequest onCreateLoadDataRequest() {
        return createRequest(0, mPageSize);
    }

    public void loadNextPage() {

    }

    @Override
    protected void onLoadSuccess(Object response) {
        List<T> objects = objectsFromResponse(response);
        mHasMoreData = objects.size() >= mPageSize;

        notifyDidLoadPageObjects(0, objects, mHasMoreData);
    }

    protected abstract ApiRequest createRequest(int pageNumber, int pageSize);

    protected abstract List<T> objectsFromResponse(Object response);

    protected void notifyDidLoadPageObjects(int pageNumber, List<T> objects, boolean hasNextPage) {

    }
}
